package org.szgb.console.zx.task.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.line.entity.SysLineLocationSystemEquipment;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class ZxTaskForm extends BeanEntity<ZxTaskForm> {
    @Serial
    private static final long serialVersionUID = 1L;
    private ZxTask task;
    private SysLineLocationSystemEquipment equipment;
    private String unit;
    private Boolean isFinish;
    private String value;
    private String imageIds;
    private Boolean isAbnormal;
    private String abnormalRemark;

    public ZxTaskForm() {
    }

    public ZxTaskForm(String id) {
        this.setId(id);
    }

    public ZxTaskForm(ZxTaskForm.ZxTaskFormFindDto zxTaskFormFindDto) {
        this.setId(zxTaskFormFindDto.getId());
        this.setTask(new ZxTask(zxTaskFormFindDto.getTaskId()));
        this.setIsFinish(zxTaskFormFindDto.getIsFinish());
        this.setIsAbnormal(zxTaskFormFindDto.getIsAbnormal());
        this.setPageDto(zxTaskFormFindDto.getPageDto());
    }

    public ZxTaskForm(ZxTaskForm.ZxTaskFormSaveDto zxTaskFormSaveDto) {
        this.setId(zxTaskFormSaveDto.getId());
        this.setTask(new ZxTask(zxTaskFormSaveDto.getTaskId()));
        this.setEquipment(new SysLineLocationSystemEquipment(zxTaskFormSaveDto.getEquipmentId()));
        this.setUnit(zxTaskFormSaveDto.getUnit());
        this.setIsFinish(zxTaskFormSaveDto.getIsFinish());
        this.setValue(zxTaskFormSaveDto.getValue());
        this.setImageIds(zxTaskFormSaveDto.getImageIds());
        this.setIsAbnormal(zxTaskFormSaveDto.getIsAbnormal());
        this.setAbnormalRemark(zxTaskFormSaveDto.getAbnormalRemark());
    }

    public ZxTaskForm(ZxTaskForm.ZxTaskFormSubmitDto zxTaskFormSubmitDto) {
        this.setId(zxTaskFormSubmitDto.getId());
        this.setIsFinish(zxTaskFormSubmitDto.getIsFinish());
        this.setValue(zxTaskFormSubmitDto.getValue());
        this.setImageIds(zxTaskFormSubmitDto.getImageIds());
        this.setIsAbnormal(zxTaskFormSubmitDto.getIsAbnormal());
        this.setAbnormalRemark(zxTaskFormSubmitDto.getAbnormalRemark());
    }

    public ZxTaskForm.ZxTaskFormView toZxTaskFormView() {
        return new ZxTaskForm.ZxTaskFormView(this);
    }

    @Data
    @Schema(title = "专项任务表单信息", description = "专项任务表单信息")
    public static class ZxTaskFormView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "设备id")
        private String equipmentId;
        @Schema(description = "设备名称")
        private String equipmentName;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "完成状态")
        private Boolean isFinish;
        @Schema(description = "数据值")
        private String value;
        @Schema(description = "图片ids")
        private String imageIds;
        @Schema(description = "是否异常")
        private Boolean isAbnormal;
        @Schema(description = "异常备注")
        private String abnormalRemark;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public ZxTaskFormView(ZxTaskForm zxTaskForm) {
            this.id = zxTaskForm.getId();
            this.taskId = zxTaskForm.getTask().getId();
            this.equipmentId = zxTaskForm.getEquipment().getId();
            this.equipmentName = zxTaskForm.getEquipment().getName();
            this.unit = zxTaskForm.getUnit();
            this.isFinish = zxTaskForm.getIsFinish();
            this.value = zxTaskForm.getValue();
            this.imageIds = zxTaskForm.getImageIds();
            this.isAbnormal = zxTaskForm.getIsAbnormal();
            this.abnormalRemark = zxTaskForm.getAbnormalRemark();
            this.createdAt = zxTaskForm.getCreatedAt();
            this.updatedAt = zxTaskForm.getUpdatedAt();
            if (zxTaskForm.getCreatedBy() != null) {
                this.createdById = zxTaskForm.getCreatedBy().getId();
                this.createdByName = zxTaskForm.getCreatedBy().getName();
            }
            if (zxTaskForm.getUpdatedBy() != null) {
                this.updatedById = zxTaskForm.getUpdatedBy().getId();
                this.updatedByName = zxTaskForm.getUpdatedBy().getName();
            }
        }

        public static List<ZxTaskForm.ZxTaskFormView> getViewListFromList(List<ZxTaskForm> zxTaskFormList) {
            return zxTaskFormList.stream().map(ZxTaskForm.ZxTaskFormView::new).toList();
        }
    }

    @Data
    @Schema(title = "专项任务表单表单项信息", description = "专项任务表单表单项信息")
    public static class ZxTaskFormItemView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "设备名称")
        private String equipmentName;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "完成状态")
        private Boolean isFinish;
        @Schema(description = "数据值")
        private String value;
        @Schema(description = "图片ids")
        private String imageIds;
        @Schema(description = "是否异常")
        private Boolean isAbnormal;
        @Schema(description = "异常备注")
        private String abnormalRemark;

        public ZxTaskFormItemView(ZxTaskForm zxTaskForm) {
            this.id = zxTaskForm.getId();
            this.equipmentName = zxTaskForm.getEquipment().getName();
            this.unit = zxTaskForm.getUnit();
            this.isFinish = zxTaskForm.getIsFinish();
            this.value = zxTaskForm.getValue();
            this.imageIds = zxTaskForm.getImageIds();
            this.isAbnormal = zxTaskForm.getIsAbnormal();
            this.abnormalRemark = zxTaskForm.getAbnormalRemark();
        }

        public static List<ZxTaskForm.ZxTaskFormItemView> getViewListFromList(List<ZxTaskForm> zxTaskFormList) {
            return zxTaskFormList.stream().map(ZxTaskForm.ZxTaskFormItemView::new).toList();
        }
    }

    @Data
    @Schema(title = "专项任务表单查询表单", description = "专项任务表单查询参数")
    public static class ZxTaskFormFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "完成状态")
        private Boolean isFinish;
        @Schema(description = "是否异常")
        private Boolean isAbnormal;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public ZxTaskForm toZxTaskForm() {
            return new ZxTaskForm(this);
        }
    }

    @Data
    @Schema(title = "专项任务表单保存表单", description = "专项任务表单保存参数")
    public static class ZxTaskFormSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "设备id")
        private String equipmentId;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "完成状态")
        private Boolean isFinish;
        @Schema(description = "数据值")
        private String value;
        @Schema(description = "图片ids")
        private String imageIds;
        @Schema(description = "是否异常")
        private Boolean isAbnormal;
        @Schema(description = "异常备注")
        private String abnormalRemark;

        public ZxTaskForm toZxTaskForm() {
            return new ZxTaskForm(this);
        }
    }

    @Data
    @Schema(title = "专项任务表单提交表单", description = "专项任务表单提交参数")
    public static class ZxTaskFormSubmitDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "完成状态")
        private Boolean isFinish;
        @Schema(description = "数据值")
        private String value;
        @Schema(description = "图片ids")
        private String imageIds;
        @Schema(description = "是否异常")
        private Boolean isAbnormal;
        @Schema(description = "异常备注")
        private String abnormalRemark;

        public ZxTaskForm toZxTaskForm() {
            return new ZxTaskForm(this);
        }
    }
}
